package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	
	import com.atomos.log.AtomosDebug;
	import com.atomos.particle.Particle;
	import com.atomos.particle.ParticleManager;
	import com.atomos.strategy.action.StackAction;
	import com.atomos.strategy.action.life.LifeAction;
	import com.atomos.strategy.action.position.EllipsisMoveAction;
	import com.atomos.strategy.emission.ByRateEmission;
	import com.atomos.strategy.initialize.StackInitializer;
	import com.atomos.strategy.initialize.life.LifeInitializer;
	import com.atomos.strategy.initialize.velocity.StreamInitializer;
	import com.atomos.system.SimpleParticleSystem;
	import com.atomos.utils.AtomosMonitorFieldsList;
	import com.bourre.collection.Iterator;
	import com.bourre.log.Logger;
	import com.bourre.utils.FlashInspectorLayout;
	import com.kairos.engine.RTBeacon;
	import com.kairos.utils.Monitor;
	import com.kairos.utils.MonitorFieldsList;	

	[SWF(width="550",height="400",frameRate="40",backgroundColor="#000000")]
	public class VortexExample extends Sprite
	{
		protected var _system : SimpleParticleSystem;
		
		public function VortexExample ()
		{
			Logger.getInstance().addLogListener( FlashInspectorLayout.getInstance(), AtomosDebug.CHANNEL );
			
			// Set the maximum step for the current animation
			RTBeacon.getInstance().setMaxStepSize( 100 );
			
			// Creating a monitor 
			var monitor : Monitor = new Monitor( 140, 60 );
			monitor.addField( MonitorFieldsList.FPS );
			monitor.addField( MonitorFieldsList.MEMORY );
			monitor.addField( AtomosMonitorFieldsList.PARTICLES );
			monitor.start();
			addChild ( monitor );
			
			// Creating the stack for particles action
			var action : StackAction = new StackAction();
			action.add( new LifeAction() );
			action.add( new EllipsisMoveAction( 50, 30, 1, 0, 6, 10, 0, .2, 0, true ) );
			
			// Creating the stack for particles initialization
			var initializer : StackInitializer = new StackInitializer();
			initializer.add ( new LifeInitializer ( 15000 ) );
			initializer.add ( new StreamInitializer ( new Point( 0, -20 ), 5 ) );
			
			// Creating an emission witch run indefinitely
			var emission : ByRateEmission = new ByRateEmission ( Particle, new Point( 300, 350 ), 10 );
			// Creating the emitter with stacks and launch the emission
			_system = new SimpleParticleSystem( initializer, action );
			_system.emit( emission ); 
			
			// Adding the draw routine
			addEventListener( Event.ENTER_FRAME, enterFrameHandler );
		}
		
		public function enterFrameHandler ( e : Event ) : void
		{
			// Clear the scene
			graphics.clear();
			graphics.lineStyle( 2, 0xffffff );

			
			// Get an iterator of all particles of the scene
			var i : Iterator = ParticleManager.getInstance().getParticles().iterator();
			var p : Particle;
					
			// For each particle in the manager we draw a circle	
			while( i.hasNext() )
			{
				p = i.next() as Particle;
				
				if( p.lastPosition.x != 300 && p.lastPosition.y != 350 )
				{
					graphics.moveTo( p.lastPosition.x, p.lastPosition.y );
					graphics.lineTo( p.position.x, p.position.y );
				}
			}
		}
	}
}